#include <51func.h>
void handler(int signum){
    printf("before signum = %d\n", signum);
    sleep(5);
    printf("after signum = %d\n", signum);
}
void siginfo(int signum,siginfo_t *info,void *p){
    printf("signum = %d\n", signum);
    printf("pid = %d, uid = %d\n", info->si_pid, info->si_uid);
}
int main(int argc, char *argv[])
{
    //signal(SIGINT,handler);//注册了2号信号
    struct sigaction act;
    memset(&act,0,sizeof(act));
    //act.sa_handler = handler;
    // 如果想要使用三参数的回调
    // 使用sa_sigaction 取代 sa_handler
    act.sa_sigaction = siginfo;
    // 除此以外，还需要在sa_flags 增加一项 SA_SIGINFO
    //act.sa_flags = SA_RESTART|SA_RESETHAND;
    act.sa_flags = SA_RESTART|SA_NODEFER|SA_SIGINFO;
    // SA_RESTART 自动重启低速系统调用
    // SA_RESETHAND 一次注册 生效一次
    // SA_NODEFER 让信号递送时，不要临时屏蔽自己
    sigemptyset(&act.sa_mask);
    sigaddset(&act.sa_mask,SIGQUIT);
    // sa_mask 额外临时屏蔽
    sigaction(SIGINT,&act,NULL);
    char buf[4096] = {0};
    ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
    printf("sret = %ld, buf = %s\n", sret, buf);
    return 0;
}

